package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ContractService contractService;
    @Reference
    private FactoryService factoryService;
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private ExtCproductService extCproductService;

    @RequestMapping(value = "/list" ,name = "新增合同下附件并分页展示附件数据")
    public String findAll(String contractId,String contractProductId,@RequestParam(value = "page",defaultValue = "1") Integer pageNum , @RequestParam(defaultValue = "10")Integer pageSize){

//        1、用来生产附件的工厂列表
//        2、某一个合同货物下附件列表分页展示
//        3、当前合同id，为了保存货物时和购销合同关联


 //        1、用来生产附件的工厂列表
//        select * from co_factory where ctype='附件'
        FactoryExample example = new FactoryExample();
        example.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(example);
        request.setAttribute("factoryList",factoryList);

//        2、查询这个购销合同货物下的附件 select * from 附件表 where contract_product_id=??
        ExtCproductExample epExample =  new ExtCproductExample();
        epExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo pageInfo = extCproductService.findAll(epExample, pageNum, pageSize);
        request.setAttribute("page",pageInfo);

//        3、当前合同id和货物id，为了保存附件时和货物、购销合同关联
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);


       return "cargo/extc/extc-list";
    }



    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/edit",name = "保存购销合同下货物信息")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto){

        try {
            String picPath = fileUploadUtil.upload(productPhoto);
            extCproduct.setProductImage(picPath);
        } catch (Exception e) {
            System.out.println("图片上传失败..............");
            e.printStackTrace();
        }


        if(StringUtils.isNotEmpty(extCproduct.getId())){
//           id不为空，应该执行修改方法
            extCproductService.update(extCproduct);
        }else{
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateTime(new Date()); //创建时间 ,为了方便排序 新的购销合同排到前面
            extCproductService.save(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId(); // 重定向到附件展示列表页面
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到修改购销合同下附件页面")
    public String toUpdate(String id,String contractId,String contractProductId){ // 附件的id
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);
//  用来生产附件的工厂列表
//        select * from co_factory where ctype='附件'
        FactoryExample example = new FactoryExample();
        example.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(example);
        request.setAttribute("factoryList",factoryList);

        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);


        return "cargo/extc/extc-update";
    }
    @RequestMapping(value = "/delete",name = "删除购销合同下附件数据")
    public String delete(String id,String contractId,String contractProductId){ // 货物id
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId; // 重定向到列表页面

    }


}
